package com.ssm.controller;

import com.ssm.entity.SysRolePer;
import com.ssm.service.SysRolePerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (SysRolePer)表控制层
 *
 * @author makejava
 * @since 2021-06-01 10:41:24
 */
@RestController
@RequestMapping("sysRolePer")
public class SysRolePerController {
    /**
     * 服务对象
     */
    @Resource
    private SysRolePerService sysRolePerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysRolePer selectOne(Integer id) {
        return this.sysRolePerService.queryById(id);
    }

    @PostMapping("setPermission/{id}/{pers}")
    public String set(@PathVariable Integer[] pers,@PathVariable Integer id){
        SysRolePer sysRolePer = new SysRolePer();
        sysRolePer.setRoleId(id);
        List<SysRolePer> sysRolePers = sysRolePerService.queryAll(sysRolePer);
        for (SysRolePer sysRolePer1:sysRolePers){
            sysRolePer1.setDel(0);
            sysRolePerService.update(sysRolePer1);
        }
        if (pers.length!=0){
            for (Integer per:pers){
                for (SysRolePer sysRolePer1:sysRolePers){
                    if (per.equals(sysRolePer1.getPerId())){
                        sysRolePer1.setDel(1);
                        sysRolePerService.update(sysRolePer1);
                    }
                }
            }
        }
        return "success";
    }

    @PostMapping("setPermission/{id}/")
    public String set0(@PathVariable Integer id){
        SysRolePer sysRolePer = new SysRolePer();
        sysRolePer.setRoleId(id);
        List<SysRolePer> sysRolePers = sysRolePerService.queryAll(sysRolePer);
        for (SysRolePer sysRolePer1:sysRolePers){
            sysRolePer1.setDel(0);
            sysRolePerService.update(sysRolePer1);
        }
        return "success";
    }


    @GetMapping("query/{id}")
    public ArrayList<Integer> queryAll(@PathVariable Integer id){
        ArrayList<Integer> arrayList = new ArrayList<>();
        SysRolePer sysRolePer = new SysRolePer();
        sysRolePer.setRoleId(id);
        sysRolePer.setDel(1);
        List<SysRolePer> sysRolePers = sysRolePerService.queryAll(sysRolePer);
        for (SysRolePer sysRolePer1:sysRolePers){
            arrayList.add(sysRolePer1.getPerId());
        }
        return arrayList;
    }

}
